package com.yenlien.traceyun.control.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.control.entity.ScienceInfo;
import com.yenlien.traceyun.control.service.ScienceInfoService;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 * 科技服务
 */
@RestController
@RequestMapping("/scienceInfo")
public class ScienceInfoController extends BaseController {

    @Autowired
    private ScienceInfoService scienceInfoService;

    /**
     * 保存与编辑
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Object save(@RequestBody ScienceInfo scienceInfo) {
        scienceInfo.setCreateTime(new Date());
        return success(scienceInfoService.save(scienceInfo));
    }

    /**
     * 根据id查询
     */
    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    public Object findById(@PathVariable("id") String id) {
        return success(scienceInfoService.findById(id));
    }


    /**
     * 根据id删除
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.GET)
    public Object deleteByPid(@PathVariable("id") String id) {
        try {
            scienceInfoService.deleteById(id);
            return success("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fail("删除失败");
    }

    /**
     * 分类类别层级查询
     */
    @RequestMapping(value = "/tree", method = RequestMethod.GET)
    public JSONArray tree() {
        return scienceInfoService.getTree();
    }

    /**
     * 列表数据查询
     */
    @RequestMapping(value = "/treeList", method = RequestMethod.GET)
    public JSONArray treeList() {
        return scienceInfoService.treeList();
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    public Object findByList(@RequestBody ScienceInfo scienceInfo) {
        if (scienceInfo.getTopId() != null && scienceInfo.getSecondId() != null) {
            return buildPage(scienceInfoService.findByPageSecond(scienceInfo));
        }
        if (scienceInfo.getTopId() != null) {
            return buildPage(scienceInfoService.findByPage(scienceInfo));
        }
        return buildPage(scienceInfoService.findByList(scienceInfo));
    }

    @RequestMapping(value = "/findByPage", method = RequestMethod.POST)
    public Object findByPage(@RequestBody ScienceInfo scienceInfo) {
        return buildPage(scienceInfoService.findByPageSecond(scienceInfo));
    }

    /**
     * 添加分类，编辑分类
     */
    @RequestMapping(value = "/saveLevel", method = RequestMethod.POST)
    public Object saveLevel(@RequestBody JSONObject param) {
        return scienceInfoService.saveLevel(param);
    }
}
